package com.aaa.bbb.mymaintain.dao;

import com.aaa.bbb.mymaintain.entity.Employee;
import com.aaa.bbb.mymaintain.entity.IssuesManager;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author max
 * @since 2023-11-03
 */
public interface IssuesManagerDao extends BaseMapper<IssuesManager> {
    @Select({
            "<script> " +
                    "SELECT "   +
                    "    issues_employee.issue_id, "   +
                    "    issues_employee.job_id, "   +
                    "    employee.name, "   +
                    "    issues_employee.detail, "   +
                    "    issues_employee.picture, "   +
                    "    explanation, "   +
                    "    issues_employee.state, "   +
                    "    issues_employee.date "   +
                    "FROM   issues_employee"   +
                    "   INNER JOIN employee ON issues_employee.job_id = employee.job_id "   +
                    "WHERE  1=1 " +
                    "  <if test='state != null and state != \"\" '>" +
                    "     AND state = #{state}" +
                    "  </if>"   +
                    "ORDER BY issues_employee.issue_id DESC " +
                    "</script>"
    })
    public List<Map<String, Object>> selectProblem(String state);

    @Select({
            "<script> " +
                    "SELECT "   +
                    "    issues_employee.issue_id, "   +
                    "    issues_employee.job_id, "   +
                    "    employee.name, "   +
                    "    employee.gender, "   +
                    "    employee.username, "   +
                    "    issues_employee.detail, "   +
                    "    explanation, "   +
                    "    issues_employee.picture, "   +
                    "    issues_employee.state, "   +
                    "    issues_employee.date "   +
                    "FROM   issues_employee"   +
                    "   INNER JOIN employee ON issues_employee.job_id = employee.job_id "   +
                    "WHERE  1=1 " +
                    "  <if test='name != null and name != \"\" '>" +
                    "     AND name = #{name}" +
                    "  </if>"   +
                    "  <if test='gender != null and gender != \"\" '>" +
                    "     AND gender = #{gender}" +
                    "  </if>"   +
                    "  <if test='state != null and state != \"\" '>" +
                    "     AND state = #{state}" +
                    "  </if>"   +
                    "</script>"
    })
    public List<Map<String, Object>> selectInfo(String name, String gender, String state);


    //问题审核不通过
    @Update(
            "<script> " +
                    "UPDATE issues_employee SET state=#{state} "  +
                    "  <if test='reason != null and reason != \"\" '>" +
                    ", unapproval_reason = #{reason} "  +
                    "  </if>"   +
                    "WHERE "  +
                    "issue_id = #{issue_id} " +
                    "</script>"
    )
    public void updateProblem(@Param("issue_id") String issue_id,@Param("state")String state,@Param("reason")String reason);

    //经费申请不通过
    @Update(
            "<script> " +
                    "UPDATE issues_employee SET state=#{state} "  +
                    "  <if test='reason != null and reason != \"\" '>" +
                    ", unfund_reason = #{reason} "  +
                    "  </if>"   +
                    "WHERE "  +
                    "issue_id = #{issue_id} " +
                    "</script>"
    )
    public void updateFund2(@Param("issue_id") String issue_id,@Param("state")String state,@Param("reason")String reason);



    @Select({
            "<script> " +
                    "SELECT "   +
                    " job_id, "   +
                    " name "   +
                    "FROM   employee "   +
                    "WHERE  1=1 " +
                    "     AND post = '维修人员' " +
                    "</script>"
    })
    public List<Map<String,Object>> selectPerson();

    @Update(
            "<script> " +
                    "UPDATE issues_employee SET maintenance_id=#{maintenance_id} "  +
                    "WHERE "  +
                    "issue_id = #{issue_id} " +
                    "</script>"
    )
    public void updateMantenanceId(@Param("issue_id") String issue_id,@Param("maintenance_id") String maintenance_id);

    @Update(
            "<script> " +
                    "UPDATE issues_employee SET state=#{state} "  +
                    "  <if test='reason != null and reason != \"\" '>" +
                    ", persist_reason = #{reason} "  +
                    "  </if>"   +
                    "WHERE "  +
                    "issue_id = #{issue_id} " +
                    "</script>"
    )
    public void updatePersist2(@Param("issue_id") String issue_id,@Param("state")String state,@Param("reason")String reason);

}
